<template>
  <div class="add-form">
    <el-dialog
      :title='FunctionName'
      :visible="dialogVisible"
      width="60%"
      :before-close="cancel"
      >
        <el-form ref="ruleForm" :model="form" :rules='rules' label-width="220px" >
          <el-form-item label="用户名"  prop='username'>
            <el-input v-model="form.username" style="width:70%;" ></el-input>
          </el-form-item>
            <el-form-item label="邮箱"  prop='email'>
            <el-input v-model="form.email" style="width:70%;"></el-input>
          </el-form-item>
          <el-form-item label="密码"  prop='password' >
            <el-input type="password" v-model="form.password " style="width:70%;"></el-input>
          </el-form-item>
          <el-form-item label="角色"  >
            <el-input v-model="form.role" style="width:70%;"></el-input>
          </el-form-item>
          <el-form-item label="权限组名称"  prop='groupId'>
            <el-select v-model="form.permission_group_id"  placeholder="请选择" style="width:70%;">
              <el-option label="超级管理员组" :value=2></el-option>
              <el-option label="编辑组" :value=3></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="联系电话">
            <el-input v-model="form.phone" style="width:70%;"></el-input>
          </el-form-item>
          <el-form-item label="介绍">
            <el-input type="textarea" v-model="form.introduction" style="width:70%;"></el-input>
          </el-form-item>
          <el-form-item >
            <el-button type="primary" @click="onSubmit(form.id)">立即创建</el-button>
            <el-button @click="cancel">取消</el-button>
          </el-form-item>
        </el-form>
    </el-dialog>,
  </div>
</template>

<script>
import { add, detail, update } from '@/api/base/users.js'
// import { add, detail } from '@/aWpi/base/users.js'
export default {
  name: 'usersAdd',
  props: {
    // dialogVisible: {
    //   type: Boolean,
    //   required: true,
    //   default: false
    // },
    FunctionName: {
      type: String,
      required: true,
      default: '功能框'
    }

  },
  data () {
    return {
      dialogVisible: false,
      form: {
        username: '',
        email: '',
        password: '',
        role: '',
        permission_group_id: '',
        phone: '',
        introduction: '',
        id: null
      },
      rules: {
        username: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        email: [{ required: true, message: '请输入', trigger: 'blur' }],
        password: [
          { required: true, message: '请输入', trigger: 'blur' },
          { min: 3, max: 9, message: '长度在3到9个字符', trigger: 'blur' }
        ],
        groupId: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  methods: {
    async onSubmit (id) {
      if (id !== null) {
        console.log('编辑')
        console.log(this.form.last_update_time)
        const editS = await update(this.form)
        console.log(editS)
        this.$message.success('编辑成功')
      } else {
        const addS = await add(this.form)
        console.log(addS)
        this.$message.success('添加成功')
      }
      this.$refs.ruleForm.resetFields()
      this.$emit('update:dialogVisible', false)
      this.$emit('Refresh')
      this.form.phone = ''
      this.form.introduction = ''
      this.permission_group_id = ''
      this.form.role = ''
      this.form.id = null
      console.log(this.permission_group_id)
    },
    // 取消
    cancel () {
      this.$refs.ruleForm.resetFields()
      // this.$emit('update:dialogVisible', false)
      this.dialogVisible = false
      this.form.phone = ''
      this.form.introduction = ''
      this.permission_group_id = 0
      this.form.role = ''
      this.form.id = null
      console.log(this.permission_group_id)
    },
    async edit (num) {
      // console.log(num)
      const { data } = await detail({ id: num })
      // console.log(data)
      // this.form = data
      this.form.username = data.username
      this.form.email = data.email
      this.form.password = data.password
      this.form.role = data.role
      this.form.permission_group_id = data.permission_group_id
      this.form.phone = data.phone
      this.form.introduction = data.introduction
      this.form.id = data.id
      // console.log(this.form)
    }
  }

}
</script>

<style lang="scss" scoped>
</style>
